<?php
namespace app\controller;

use app\BaseController;

/**
 * 再回首专属控制器
 */
class Zh extends BaseController
{
    public function server_list(){
        //if (!$this->checkAuth()) return pr(300, API_NO_AUTH);
        $page = request()->post('page');
        $limit = request()->post('limit');
        $conditions = request()->post('conditions');
        try {
            $sum = 0;
            $obj = $this->M("mod_server_detail")
                ->alias('msd')
                 ->field("msd.id as sd_id,msd.mod_id,msd.ip_addr as ip,svl.version,msd.last_modify_time as time,msd.name,msd.level,msd.stat,sm.description,sm.uid,u.nickname as uname")
                ->join("mod sm",'sm.id = msd.mod_id')
                ->leftJoin("user u", "u.id = sm.uid")
                ->leftJoin("server_version_list svl", "svl.id = msd.version_id");
             if (!empty($conditions) && count($conditions) > 0) {
                foreach($conditions as $v){
                    if($v['type'] == "like"){
                        $obj = $obj->where($v['field'], 'like', '%' . $v['value'] . '%');
                    } else{
                        $obj = $obj->where($v['field'], $v['type'], $v['value']);
                    }
                }
            } 
            $obj->order('msd.last_modify_time', 'desc');
            if ($page == 1) $sum = $obj->count();
            $obj->page($page, $limit);
            $list = $obj->select()->toArray();
            $list['_extra'] = ['sum' => $sum, 'table' => "mod_server_detail msd,mod sm,user u,server_version_list svl"];
            return pr(200, API_SUCCESS, $list);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return pr(301, API_DB_ERROR);
        }
    }
    
    //服列表状态修改
    public function update_server_state(){
        //if (!$this->checkAuth()) return pr(300, API_NO_AUTH);
        $sd_id = request()->post('sd_id');
        $stat = request()->post('stat');
        $this->M('mod_server_detail')->where(['id'=>$sd_id])->update(['stat'=>$stat]);
        return pr(200, API_SUCCESS);
    }

    public function isadmin(): string
    {
        return json_encode(['a'=>'N','b'=>'N','c'=>'N']);
    }

    /**
     * 设置公告内容
     */
    public function setnotice(){
        $u = $this->getOperator();
        $content = request()->post('Content');
    }
    /**
     * 资源加精
     */
    public function boutique(){
        $id = request()->post('Id');
        $type = request()->post("Type");//World Mod
        $Boutique = request()->get('Boutique');//评分
        $u = $this->getOperator();
        //权限检查
        $this->M('document')->where(['id'=>$id])->update(['boutique'=>$Boutique]);
        return pr(200);
    }

    /**
     * 取消资源展示
     */
    public function hide(){
        $id = request()->post('Id');
        $type = request()->post("Type");//World Mod
        $u = $this->getOperator();
        //权限检查
        $this->M('document')->where(['id'=>$id])->update(['state'=>0]);
        return pr(200);
    }

    /**
     * 资源删除
     */
    public function deleteFile(){
        $id = request()->post('Id');
        $type = request()->post("Type");//World Mod
        $u = $this->getOperator();
        //权限检查
        $this->M('document')->where(['id'=>$id])->update(['state'=>0]);
        return pr(200);
    }

    public function getOperator(){
        $operator = request()->post('Operater');
        $f = $this->M('user')->where(['token'=>$operator])->field("id")->find();
        return $f;
    }
    /**
     * 联机检查
     **/
    public function forceCheckCode(){
        $code = request()->post('code');//?
        $token = request()->post('token');//?
        return pr(200,"Force");
    }
    
    /**
     * 联机检查
     **/
    public function checkCode(){
        $code = request()->post('code');//?
        $token = request()->post('token');//?
        return pr(200,"Force");
    }

    /**
     * 获取用户列表
     * */
    public function userList(){
        $action = request()->post('Action');//GetUserList  UpdateLockState ResetPassword
        $key = request()->post('SearchKey');
        $type = request()->post('SearchType');
        $u = $this->getOperator();
        $page = request()->post('Cursor',1,'int');
        $id = request()->post('Id',0,'int');
        $duration = request()->post('Duration');
        $reson = request()->post('Reason');
        $lockStat = request()->post('LockState');
        
        if($action == 'GetUserList'){
            $list = $this->M('user')->field("id,nickname,email")->page($page,10)->select()->toArray();
            return pr(300,'',$list);
        }
        return pr(300);
    }
}
